package com.adcc.ags.cgw.service;

import com.adcc.ags.cgw.entity.msg.TrafficMsg;
import com.adcc.ags.cgw.managed.uplink.UplinkForward;
import com.adcc.utility.log.Log;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 消息报文服务
 */
public class MsgSaverServiceImpl extends MsgSaverService {

    @Override
    public void onReceive(Object message) {
        super.onReceive(message);
        try {
            if (message instanceof TrafficMsg) {
                TrafficMsg msg = (TrafficMsg) message;
                String strJSON = new ObjectMapper().writeValueAsString(msg);
                try {
                    UplinkForward.getInstance().sendMsgToDB(strJSON.getBytes());
                    Log.info(MsgSaverService.class.getName(), "send message to dbforward message:\r\n" + strJSON);
                } catch (Exception ex) {
                    Log.error(MsgSaverService.class.getName(), "send message to dbforward failed message:\r\n" + strJSON);
                }
            } else {
                unhandled(message);
            }
        } catch (Exception ex) {
            Log.error(MsgSaverService.class.getName(), "onReceive() error", ex);
        }
    }
}
